using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Entity.Infrastructure;
using ShopPowerTemplate.Services.Properties;
using ShopPowerTemplate.Entities;

namespace ShopPowerTemplate.Services
{
    public class OptionService : OptionServiceBase, IOptionService
    {
		public OptionService(IEntitiesDB entitiesDB) : base(entitiesDB) { }

		//Custom code here

        public override List<Option> FindAvailableOptionList<TKey>(List<Option> notAvailableList, string searchValue, System.Linq.Expressions.Expression<Func<Option, TKey>> orderBy, bool ascending, int pageIndex, int pageSize)
        {
            IEnumerable<int> notAvailableIDList = notAvailableList.Select(na => na.Id);
            IQueryable<Option> query = EntitiesDB.OptionSet.Include("OptionType").Where(a => !notAvailableIDList.Any(id => id == a.Id)).Where(x => string.IsNullOrEmpty(searchValue) || x.Name.Contains(searchValue)
                || x.Description.Contains(searchValue));

            query = ascending ? query.OrderBy(orderBy) : query.OrderByDescending(orderBy);

            if (pageSize > 0)
                query = query.Skip(pageSize * pageIndex).Take(pageSize);

            return query.ToList();
        }
	}
}	

